package com.tianji.learning.controller;

import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.domain.query.PageQuery;
import com.tianji.learning.domain.dto.LearningPlanDTO;
import com.tianji.learning.domain.vo.LearningLessonVO;
import com.tianji.learning.domain.vo.LearningPlanPageVO;
import com.tianji.learning.service.ILearningLessonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 学生课程表 前端控制器
 * </p>
 *
 * @author yang
 * @since 2024-06-20
 */
@Api(tags = "课表相关接口")
@RestController
@RequestMapping("/lessons")
@Slf4j
@RequiredArgsConstructor
public class LearningLessonController {

    private final ILearningLessonService learningLessonService;

    @GetMapping("/page")
    @ApiOperation("分页查询学生课程表")
    public PageDTO<LearningLessonVO> queryLessonPage(PageQuery pageQuery) {
        log.info("分页查询学生课程表，参数：{}", pageQuery);
        return learningLessonService.PageAll(pageQuery);
    }

    @GetMapping("/now")
    @ApiOperation("查询当前正在学习的课程")
    public LearningLessonVO getNowLesson() {
        return learningLessonService.getNowLesson();
    }

    @DeleteMapping("/{courseId}")
    @ApiOperation("删除课程")
    public void deleteLesson(@PathVariable("courseId") List<Long> courseId) {
        learningLessonService.refundUserLessons(courseId);
    }

    @GetMapping("/{courseId}/valid")
    @ApiOperation("查询课程是否有效")
    Long getLessonId(@PathVariable("courseId") Long courseId) {
        return learningLessonService.getLessonId(courseId);
    }

    @GetMapping("/{courseId}")
    @ApiOperation("查询课程状态")
    LearningLessonVO getLessonStatus(@PathVariable("courseId") Long courseId) {
        return learningLessonService.getLessonStatus(courseId);
    }

    /**
     * 统计课程学习人数
     * @param courseId 课程id
     * @return 学习人数
     */
    @GetMapping("/{courseId}/count")
    @ApiOperation("统计课程学习人数")
    Integer countLearningLessonByCourse(@PathVariable("courseId") Long courseId){
        return learningLessonService.countLearningLessonByCourse(courseId);
    }

    @ApiOperation("创建学习计划")
    @PostMapping("plans")
    public void createPlans(@Valid @RequestBody LearningPlanDTO planDTO){
        learningLessonService.createPlans(planDTO);
    }

    @ApiOperation("查询我的学习计划")
    @GetMapping("plans")
    public LearningPlanPageVO queryMyPlans(PageQuery pageQuery){
        return learningLessonService.queryMyPlans(pageQuery);
    }


}
